package com.xiaotao.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
//import java.util.Map;

@Configuration
public class RabbitConfig {
    // 交换机
    public static final String ORDER_EXCHANGE = "order.exchange";
   public static final String EXCHANGE_CHECK_ORDER_STATUS="exchangeCheckOrder";//延迟队列交换机

    // 队列
    public static final String ORDER_CREATE_QUEUE = "order.create.queue";
    public static final String QUEUE_CHECK_ORDER_STATUS="queueCheckOrder";//延迟队列
   public static final Integer ORDER_TTL = 60*1000*15;//

    // routing key
    public static final String ORDER_CREATE_KEY = "order.create";
    public static final String ROUTING_KEY_CHECK_ORDER = "routingCheckOrder";//路由键

    @Bean
    public TopicExchange orderExchange()
    {
        return new TopicExchange(ORDER_EXCHANGE);
    }

    // 异步订单生成队列
    @Bean
    public Queue orderCreateQueue() {
        return new Queue(ORDER_CREATE_QUEUE, true);
    }

    @Bean
    public Binding orderCreateBinding()
    {
        return BindingBuilder.bind(orderCreateQueue())
                        .to(orderExchange())
                        .with(ORDER_CREATE_KEY);
    }

    // 延迟取消队列 (TTL + DLX)
    @Bean
    public CustomExchange checkOrderExchange(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-delayed-type", "topic");//延迟消息
        return new CustomExchange(EXCHANGE_CHECK_ORDER_STATUS, "x-delayed-message", true, false, map);//延迟队列交换机
    }//

    @Bean
    public Queue checkOrderQueue(){
        return new Queue(QUEUE_CHECK_ORDER_STATUS);
    }//延迟队列
    @Bean
    public Binding checkOrderQueueToCheckOrderExchange(){
        return BindingBuilder.bind(checkOrderQueue()).to(checkOrderExchange()).with(ROUTING_KEY_CHECK_ORDER).noargs();
    }
}
